package zj.com.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zj.com.interfaze.Callback;
import zj.com.model.InspectItem;
import zj.com.util.LogUtils;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/3.
 */
public class ChoiceJcxAdapter extends BaseAdapter{

    public List<InspectItem> list;
    public Context context;
    public LayoutInflater inflater;

    public boolean isEdit = false;
    public Map<String,InspectItem> selectMap = new HashMap<String,InspectItem>();
    public ChoiceJcxAdapter(Context context,List<InspectItem> list){

        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      final  ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder   = new ViewHolder();
            convertView = inflater.inflate(R.layout.adapter_choice_jcx, null);
            viewHolder.name_tv = (TextView) convertView.findViewById(R.id.title_tv);
            viewHolder.type_tv = (TextView) convertView.findViewById(R.id.type_tv);
            viewHolder.view = convertView;
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


       final  InspectItem item = list.get(position);
        viewHolder.name_tv.setText(item.getInspectItemName());
        viewHolder.type_tv.setText("[" + item.getZyName() + "]");

        if (selectMap.get(item.getInspectItemCode()) == null) {
            viewHolder.view.setBackgroundColor(Color.TRANSPARENT);
            //viewHolder.name_tv.setTextColor(Color.parseColor("#959595"));
        }else{


            viewHolder.view.setBackgroundColor(Color.parseColor("#e8e8e8"));
            //viewHolder.name_tv.setTextColor(Color.parseColor("#010101"));
        }


        viewHolder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (selectMap.get(item.getInspectItemCode()) == null) {
                    //uncheck
                    viewHolder.view.setBackgroundColor(Color.parseColor("#e8e8e8"));
                    //viewHolder.name_tv.setTextColor(Color.parseColor("#010101"));
                    selectMap.put(item.getInspectItemCode(), item);
                } else {
                    viewHolder.view.setBackgroundColor(Color.TRANSPARENT);
                   // viewHolder.name_tv.setTextColor(Color.parseColor("#959595"));
                    selectMap.remove(item.getInspectItemCode());
                }
                if (callback != null) {
                    callback.call();
                }

            }
        });
        return convertView;
    }
    private static class ViewHolder {
        TextView name_tv;
        TextView type_tv;
        View view ;
    }

    public void setDataChange(List<InspectItem> list){


        this.list = list;
        notifyDataSetChanged();
    }
    public Callback callback;
    public void setCallback(Callback call){
        this.callback = call;
    }
    public Map<String,InspectItem> getSelectMap(){

        return selectMap;
    }


}
